<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>选择用户角色</title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
	</head>
	<body class="pear-container">

    <input id="aid" value="" style="display: none;">
        <form class="layui-form" action="javascript:void (0)">
            <div class="layui-card">
                <div class="layui-card-body">
                    <table id="user-table" lay-filter="user-table"></table>
                </div>
            </div>
            <div class="bottom">
                <div class="button-container">
                    <button class="pear-btn pear-btn-primary pear-btn-sm" id="user-save" lay-filter="user-save">
                        <i class="layui-icon layui-icon-ok"></i>
                        提交
                    </button>
                </div>
            </div>
        </form>

        <script type="text/html" id="user-toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" id="save"  lay-event="add" style="display: none">
                <i class="layui-icon layui-icon-add-1"></i>
                保存
            </button>
        </script>

        <script type="text/html" id="role-cate">
            {{#if (d.cate == 'menu') { }}
            <span>菜单</span>
            {{# }else if(d.cate == 'permission'){ }}
            <span>权限</span>
            {{# } }}
        </script>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
        <script src="../../js/baseUrl.js"></script>
        <script src="../../js/cookieUtils.js"></script>
        <script src="../../component/pear/module/ajaxRequest.js"></script>
        <script src="../../js/jquery-3.2.1.min.js"></script>
        <script src="../../js/permission.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery', 'drawer', 'dropdown','ajaxRequest','toast'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let drawer = layui.drawer;
				let dropdown = layui.dropdown;
                let ajaxRequest = layui.ajaxRequest;
                let toast = layui.toast;

				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '角色名称',
							field: 'name',
							align: 'center'
						},
						{
							title: '角色类型',
							field: 'cate',
							align: 'center',
                            templet: '#role-cate'
						},
                        {
                            title: '角色备注',
                            field: 'remark',
                            align: 'center'
                        }
					]
				]

                table = $.extend(table, { config: { checkName: 'checked' } });
                var aid = '';
				$(function () {
                    aid = $('#aid').val();
                    table.render({
                        elem: '#user-table',
                        url: baseUrl + "/admin/user/getSysRoleList?userId="+aid,
                        headers: {'Authorization': tokenPrefix + token},
                        page: true,
                        cols: cols,
                        skin: 'line',
                        toolbar: '#user-toolbar',
                        limit : 15,
                        defaultToolbar: [],
                        done: function(res, curr, count) {
                            // 隐藏头部工具栏
                            $(".layui-table-tool").css('display','none')
                            // 添加权限验证
                            // validaPermission();
                            // validaRole();
                        }
                    });
                });


				table.on('toolbar(user-table)', function(obj) {
				    if (obj.event === 'add') {
						window.add(obj);
					}
				});

				$("#user-save").click(function () {
                    $('#save').click();
                });

                window.add = function(obj) {
                    let data = table.checkStatus(obj.config.id).data;
                    if (data.length === 0) {
                        layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    let ids = "";
                    for (let i = 0; i < data.length; i++) {
                        ids += data[i].id + ",";
                    }
                    ids = ids.substr(0, ids.length - 1);
                    // console.log(ids);
                    layer.confirm('确定要给当前用户添加这些角色吗', {
                        icon: 3,
                        title: '提示'
                    }, function(index) {
                        layer.close(index);

                        parent.layui.table.reload("user-table");

                        let loading = layer.load();
                        var res = ajaxRequest.sendPost("/admin/user/addRole",{'aid':aid,'rids':ids},false,true);
                        if (res){
                            setTimeout(function() {
                                layer.close(loading);
                                toast.success({title: 'Success',message: res.msg,position: 'topRight'});
                                parent.layer.close(parent.layer.getFrameIndex(window
                                    .name)); //关闭当前页
                            }, 1500);

                        }
                    });
                };
			});


		</script>
	</body>
</html>
